using System;
using System.Collections;
using System.Data.Linq.Mapping;
using System.Runtime.Serialization;
using System.Transactions;
using System.Web;
using Portal.Data.Helper;
using Portal.Data.Security;
using Portal.Core.Binding.FormBuilder;
using Portal.Core.Helper;
using System.Linq;

namespace Portal.Data.Entity
{
    [Table(Name = "dbo.User"), Serializable, DataContractAttribute]
    public class PortalUser : BaseObject
    {
        #region Ctor
        public PortalUser()
        {
            DateOfBirth = DateTime.Now;
        }
        #endregion

        #region Properties
        public int ID { get { return User_Id; } set { User_Id = value; } }

        [SkipBinding]
        [ColumnAttribute(Name = "User_Id", IsPrimaryKey = true, IsDbGenerated = true)]
        public int User_Id { get; set; }

        [ColumnAttribute(Name = "Title")]
        public string Title { get; set; }

        [ColumnAttribute(Name = "Name")]
        public string Name { get; set; }

        [ColumnAttribute(Name = "FullName")]
        public string FullName { get; set; }

        [ColumnAttribute(Name = "Password")]
        public string Password { get; set; }

        [ColumnAttribute(Name = "Email")]
        public string Email { get; set; }

        [ColumnAttribute(Name = "SecondEmail")]
        public string SecondEmail { get; set; }

        [ColumnAttribute(Name = "Address")]
        public string Address { get; set; }

        [ColumnAttribute(Name = "Telphone")]
        public string Telphone { get; set; }

        [ColumnAttribute(Name = "Mobile")]
        public string Mobile { get; set; }

        [ColumnAttribute(Name = "Fax")]
        public string Fax { get; set; }

        [ColumnAttribute(Name = "CompanyName")]
        public string CompanyName { get; set; }

        [ColumnAttribute(Name = "CompanyAddress")]
        public string CompanyAddress { get; set; }

        [ColumnAttribute(Name = "CompanyEmail")]
        public string CompanyEmail { get; set; }

        [ColumnAttribute(Name = "Avatar")]
        public string Avatar { get; set; }

        [ColumnAttribute(Name = "Hobby")]
        public string Hobby { get; set; }

        [ColumnAttribute(Name = "Gender")]
        public int? Gender { get; set; }

        [ColumnAttribute(Name = "DateOfBirth")]
        public DateTime? DateOfBirth { get; set; }

        [ColumnAttribute(Name = "Active")]
        public bool Active { get; set; }


        #endregion

        #region Methods
        public static PortalUser FindByPrimaryKey(object objectid)
        {
            return FindByPrimaryKey<PortalUser>(objectid);
        }

        public static PortalUser LogIn(string userName, string password)
        {
            var result = LINQHelper.GetFirst<PortalUser>("select * from [User] where Name={0} and Password={1}", userName, Tools.Hash(password));
            return result;
        }

        public static void ChangePassword(PortalUser user, string newPassword)
        {
            user.Password = Tools.Hash(newPassword);
            ORMHelper.InstanceIgnorePermission.Update(user);
            //update current session value
            HttpContext.Current.Session["Portal@User"] = user;
        }

        public static IList ListAll()
        {
            using (var context = new CmsDataContext())
            {
                return context.PortalUsers.Where(o => o.Name.ToLower() != "Administrator")
                    .OrderBy(o => o.Name).ToList();
            }
        }
        public static PortalUser FindByIdAndPasswordHash(int id, string passwordHash)
        {
            var user = FindByPrimaryKey(id);
            if (user != null && user.Password == passwordHash)
                return user;
            return null;
        }
        public static IList Search(string key, int pagesize, int pageindex, out int count)
        {
            string sql = "select * from [User] where Name!='Administrator' ";
            if (!string.IsNullOrEmpty(key))
            {
                sql += string.Format(" and (Name like N'%{0}%' or FullName like N'%{0}%')", InjectionHelper.Safely(key));
            }
            return LINQHelper.SlicedFindAll<PortalUser>(sql, "order by User_Id desc", pageindex * pagesize, pagesize, out count);
        }
        public static bool IsNameExists(string name)
        {
            return LINQHelper.GetFirst<int>("select count(*) from PortalUser where Name = {0}", name) > 0;
        }
        #endregion Methods

        #region Extension Properties
        public bool IsAdministrator
        {
            get { return Name.Equals("Administrator", StringComparison.OrdinalIgnoreCase); }

        }
        public string DisplayName
        {
            get { return string.Format("{0}-{1}", Name, FullName); }

        }
        #endregion
    }
}
